package com.system.vote.admin.security;


import com.system.vote.admin.exception.CustomJWTException;
import com.system.vote.common.Contants;
import com.system.vote.common.utils.StringUtils;
import com.system.vote.core.contant.JWTConstant;
import com.system.vote.admin.entity.SysAccount;
import com.system.vote.admin.service.SysAccountService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.AntPathMatcher;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class JwtAuthenticationFilter extends BasicAuthenticationFilter {

    @Autowired
    private SysAccountService service;

    @Autowired
    private MyUserDetailServiceImpl myUserDetailService;


    public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(authenticationManager);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
//        super.doFilterInternal(request, response, chain);
        String token = request.getHeader("token");
        //如果token是空则放行
        if (StringUtils.isEmpty(token) || StringUtils.checkUrl(Contants.URL_WHITELIST, request.getRequestURI())) {
            chain.doFilter(request, response);
        } else {
            CheckResult checkResult = JwtUtil.validateJWT(token);
            if (!checkResult.isSuccess()) {
                switch (checkResult.getErrCode()) {
                    case JWTConstant.JWT_ERRCODE_NULL:
                        throw new CustomJWTException("Token不存在");
                    case JWTConstant.JWT_ERRCODE_FAIL:
                        throw new CustomJWTException("Token验证不通过");
                    case JWTConstant.JWT_ERRCODE_EXPIRE:
                        throw new CustomJWTException("Token过期");

                }
            }
            Claims claims = JwtUtil.parseJWT(token);
            String username = claims.getSubject();
            SysAccount sysAccount = service.getByUserName(username);

            UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(sysAccount, null, myUserDetailService.getUserAuthority(sysAccount.getId()));
            SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
            chain.doFilter(request, response);
        }
    }
}
